#!/bin/sh

# Configure script for Xymon CLIENT
# $Id: configure.client 7044 2012-07-13 16:53:23Z storner $

echo ""
echo "Configuration script for Xymon client"
echo ""

if test -r Makefile
then
	echo "Already configured - remove Makefile to re-configure"
	exit 1
fi

echo "This script asks a few questions and builds a Makefile to compile Xymon"
echo ""

if test "$MAKE" = ""; then
	MAKE=make
fi
echo "Checking your make-utility"
MAKEVER=`$MAKE -version 2>&1 | head -n 1 | awk '{print $1 " " $2}'`
if test "$MAKEVER" != "GNU Make"
then
	echo "GNU make is required to build Xymon."
	echo "If it is available as \"gmake\", run configure as: 'MAKE=gmake $0'"
	exit 1
fi
echo ""

echo "Xymon normally keeps all of the client configuration files"
echo "on the Xymon server. If you prefer, it is possible to use"
echo "a local client configuration file instead - if so, answer"
echo "'client' to the next question."
echo "NB: Local configuration requires the PCRE libs on each host."
echo ""
echo "Server side client configuration, or client side [server] ?"
if test -z "$CONFTYPE"
then
	read CONFTYPE
fi
if test -z "$CONFTYPE"
then
	CONFTYPE="server"
fi
echo ""

if test "$CONFTYPE" = "client"
then
	echo "Checking for the PCRE libraries"
	. build/pcre.sh
	echo ""
fi
echo ""

MAKE="$MAKE -s" ./build/lfs.sh
if test $? -eq 0; then
	LFS="-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
else
	LFS=""
fi
echo ""; echo ""

MAKE="$MAKE -s" . ./build/clock-gettime-librt.sh
echo ""; echo ""

echo "What userid will be running Xymon [xymon] ?"
if test -z "$XYMONUSER"
then
	read XYMONUSER
fi
if test -z "$XYMONUSER"
then
	XYMONUSER="xymon"
fi

if test -z "$XYMONTOPDIR"
then
	if test "`uname -s`" = "Darwin"
	then
		# Use "dscl" for locating user information. From Isaac Vetter
		# http://www.xymon.com/archive/2008/02/msg00173.html
		USERDATA="`dscl . -list /Users  | grep $XYMONUSER`"
		if test "$USERDATA" != ""
		then
			echo "Found Directory entry for user: $USERDATA"
		else
			echo "FAILURE: The user $XYMONUSER does not exist locally. Create user and try again."
			exit 1
		fi
		echo ""; echo ""
		HOMEDIR="`dscl . -read /Users/$XYMONUSER | grep HomeDirectory | awk '{print $2}'`"
	else
		USERDATA=`getent passwd $XYMONUSER 2>/dev/null || ypmatch "${XYMONUSER}" passwd || grep "^${XYMONUSER}:" /etc/passwd`
		if test $? -eq 0
		then
			echo "Found passwd entry for user $USERDATA"
		else
			echo "FAILURE: The user $XYMONUSER does not exist. Create user and try again."
			exit 1
		fi
		echo ""; echo ""
		HOMEDIR="`echo $USERDATA|cut -d: -f6`"
	fi
else
	HOMEDIR="$XYMONTOPDIR"
fi

echo "Where do you want the Xymon installation [${HOMEDIR}] ?"
if test -z "$XYMONTOPDIR"
then
	read XYMONTOPDIR
fi
if test -z "$XYMONTOPDIR"
then
	XYMONTOPDIR=${HOMEDIR}
fi
if test -d "$XYMONTOPDIR"
then
	echo "OK, will configure to use $XYMONTOPDIR as the Xymon toplevel directory"
else
	echo "WARNING: $XYMONTOPDIR does not exist."
fi
echo ""; echo ""

echo "What is the IP-address of your Xymon server [127.0.0.1] ? "
if test -z "$XYMONHOSTIP"
then
	read XYMONHOSTIP
fi
if test -z "$XYMONHOSTIP"
then
	echo "** NOTE: Using 127.0.0.1 (loopback), but it is probably not what you want **"
	XYMONHOSTIP=127.0.0.1
fi
echo ""; echo ""

XYMONHOSTOS=`uname -s | tr '[A-Z]' '[a-z]'`
if test "$XYMONHOME" = ""; then
	XYMONHOME=$XYMONTOPDIR/client
fi

echo "# Toplevel Makefile for Xymon"    > Makefile

echo "BUILDTOPDIR=\`pwd\`"               >>Makefile
echo "CLIENTONLY = yes"                  >>Makefile
if test "$CONFTYPE" = "client"
then
	echo "LOCALCLIENT = yes"         >>Makefile
else
	echo "LOCALCLIENT = no"          >>Makefile
fi
echo ""                                  >>Makefile
echo "# configure settings for Xymon"    >>Makefile
echo "#"                                 >>Makefile
echo "# Toplevel dir"                    >>Makefile
echo "XYMONTOPDIR = $XYMONTOPDIR"        >>Makefile
echo "# Server home dir for etc/, www/"  >>Makefile
echo "XYMONHOME = $XYMONHOME"            >>Makefile
echo ""                                  >>Makefile
echo "# Xymon settings follows"          >>Makefile
echo "#"                                 >>Makefile
echo "# Username running Xymon"          >>Makefile
echo "XYMONUSER = $XYMONUSER"            >>Makefile
echo "# Xymon server IP-address"         >>Makefile
echo "XYMONHOSTIP = $XYMONHOSTIP"        >>Makefile
echo "# Large File Support settings"     >>Makefile
echo "LFSDEF = $LFS"                     >>Makefile
echo "LIBRTDEF = $LIBRTDEF"              >>Makefile

echo "" >>Makefile
if test -r build/Makefile.`uname -s`
then
	echo "include build/Makefile.`uname -s`" >>Makefile
	echo ""
	echo "Using `uname -s` Makefile settings"
	echo ""
else
	echo "include build/Makefile.generic" >>Makefile
	echo ""
	echo "Using GENERIC Makefile settings"
	echo ""
	echo "If this fails, change the compile settings in Makefile"
	echo ""
	echo "I would appreciate it if you send the changes to"
	echo "xymon-owner@xymon.com so I can include it in the next version."
	echo ""
fi
echo "" >>Makefile
if test "$INSTALLBINDIR" != ""; then
	echo "INSTALLBINDIR = $INSTALLBINDIR"   >>Makefile
fi
if test "$INSTALLETCDIR" != ""; then
	echo "INSTALLETCDIR = $INSTALLETCDIR"   >>Makefile
fi
if test "$INSTALLEXTDIR" != ""; then
	echo "INSTALLEXTDIR = $INSTALLEXTDIR"   >>Makefile
fi
if test "$INSTALLTMPDIR" != ""; then
	echo "INSTALLTMPDIR = $INSTALLTMPDIR"   >>Makefile
fi
if test "$INSTALLWEBDIR" != ""; then
	echo "INSTALLWEBDIR = $INSTALLWEBDIR"   >>Makefile
fi
if test "$INSTALLWWWDIR" != ""; then
	echo "INSTALLWWWDIR = $INSTALLWWWDIR"   >>Makefile
fi
echo "" >>Makefile
if test "$CONFTYPE" = "client"
then
	echo "# PCRE settings"                   >>Makefile
	echo "PCREINCDIR = -I$PCREINC"           >>Makefile
	echo "PCRELIBS = -L$PCRELIB -lpcre"      >>Makefile
	echo "RPATHVAL += ${PCRELIB}"            >>Makefile
fi
echo "#"                                 >>Makefile
echo "# Add local CFLAGS etc. settings here" >>Makefile
echo "" >>Makefile
echo "include build/Makefile.rules" >> Makefile
echo "" >> Makefile

echo ""; echo ""
echo "Created Makefile with the necessary information to build Xymon"
echo "Some defaults are used, so do look at the Makefile before continuing."
echo ""

echo "Configuration complete - now run $MAKE (GNU make) to build the tools"

exit 0

